<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import protobuf from "protobufjs";
export default {
  data() {
    return {
      info: {},
      sdk:null,//webrtc推流媒体
    };
  },
  created() {
    this.subscribe();
  },
  methods: {
    ...mapMutations("SystemModule", ["setElectricity", "setInfo"]),
    subscribe() {
      this.$mqtt.subscribe("dashboard/systemInfo", (msg) => {
        // JSON解析
        // const message = JSON.parse(msg.toString());
        // this.setInfo(message);
        // this.setElectricity(message.battery.electricity);

        // probuffer解析
        console.log("收到消息：" + msg.toString());
        protobuf.load("/proto/message.proto", (err, root) => {
          if (err) {
            console.error("Failed to load proto file:", err);
            return;
          }
          const SystemInfo = root.lookupType("SystemInfo");
          try {
            const message = SystemInfo.decode(msg);
            console.log("解析protobuf数据成功：", message);
            this.setInfo(message);
            this.setElectricity(message.battery.electricity);
          } catch (err) {
            console.error("Error decoding message:", err);
          }
        });
      });
    },
    
    unsubscribe() {
      this.$mqtt.unsubscribe("my/topic");
    },
    // saveData() {
    //   sessionStorage.setItem(
    //     "info",
    //     JSON.stringify(this.$store.SystemModule.state.info)
    //   );
    // },
  },
};
</script>

<style></style>
